$(document).ready(function () {
 
  var qian, zhong, hou, calculatorCount, result, calculator;
  $("input").click(function () {
    $("#calculator-text").append($(this).val());
    var btnValue = $(this).val();
    calculator = $("#calculator-text").text();
    if ((btnValue === "/") || (btnValue === "*") || (btnValue === "-") || (btnValue === "+")) {
      calculatorCount = calculator.length;
      qian = Number(calculator.substring(0, calculatorCount - 1));
      zhong = calculator.substring(calculatorCount - 1, calculatorCount);
    }
    btnValueEnter(btnValue, calculatorCount, calculator, qian, zhong, hou, result);
    btnValueAc(btnValue);
    $(this).blur();
  });
  $(document).keypress(function (e) {
    switch (String(e.keyCode)) {
      case '13':
        $("input[value='Enter']").click();
        break;
      case '42':
        $("input[value='*']").click();
        break;
      case '43':
        $("input[value='+']").click();
        break;

      case '46':
        $("input[value='.']").click();
        break;
      case '47':
        $("input[value='/']").click();
        break;
      case '48':
        $("input[value='0']").click();
        break;
      case '49':
        $("input[value='1']").click();
        break;
      case '50':
        $("input[value='2']").click();
        break;
      case '51':
        $("input[value='3']").click();
        break;
      case '52':
        $("input[value='4']").click();
        break;
      case '53':
        $("input[value='5']").click();
        break;
      case '54':
        $("input[value='6']").click();
        break;
      case '55':
        $("input[value='7']").click();
        break;
      case '56':
        $("input[value='8']").click();
        break;
      case '57':
        $("input[value='9']").click();
        break;
      default:
    }
  });
});
/**
 * 用於計算結果
 * @type type 
 */
function btnValueEnter (btnValueEnter, calculatorCount, calculator, qian, zhong, hou, result) {
  if (btnValueEnter === "Enter") {
    hou = Number(calculator.substring(calculatorCount, calculator.length - 5));
    switch (zhong) {
      case "+":
        result = ((qian * 1000) + (hou * 1000)) / 1000;
        break;
      case "-":
        result = qian - hou;
        break;
      case "*":
        result = ((qian * 1000) * (hou * 1000)) / 1000000;
        break;
      case "/":
        result = String(qian / hou).substring(0, 10);
        break;
      case "%":
        result = String(qian % hou).substring(0, 10);
        break;
    };
    $("#calculator-text").text(result);
  }
}
/**
 * 這是清除
 * @param {type} btnValue
 * @returns {undefined}
 */
function btnValueAc (btnValue) {
  if (btnValue === "AC") {
    $("#calculator-text").text("");
  }
}

 